<?php

/**
 * This is the model class for table "announcement".
 *
 * The followings are the available columns in table 'announcement':
 * @property integer $id
 * @property string $title
 * @property string $content
 * @property string $description
 * @property integer $status
 * @property integer $event_fg
 * @property integer $position
 * @property integer $view_no
 * @property integer $join_no
 * @property string $created
 * @property string $updated
 * @property integer $user_id
 * @property string $event_end_date
 *
 * The followings are the available model relations:
 * @property Users $user
 * @property AnnouncementAssign[] $announcementAssigns
 * @property AnnouncementView[] $announcementViews
 * @property Comments[] $comments
 */
class AnnouncementBase extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return AnnouncementBase the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'announcement';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('title, content, description, created, user_id, event_end_date', 'required'),
			array('status, event_fg, position, view_no, join_no, user_id', 'numerical', 'integerOnly'=>true),
			array('title', 'length', 'max'=>100),
			array('updated', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, title, content, description, status, event_fg, position, view_no, join_no, created, updated, user_id, event_end_date', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'owner' => array(self::BELONGS_TO, 'Account', 'user_id'),
			'member_asign' => array(self::HAS_ONE, 'AnnouncementAssign', 'announcement_id', 'joinType' => 'INNER JOIN'),
			//'member_view' => array(self::HAS_ONE, 'AnnouncementView', '', 'on' => 'member_asign.announcement_id = member_view.announcement_id and member_asign.member_id = member_view.user_id', 'joinType' => 'LEFT JOIN')
		);
	}

	public function getAsignByMember($member_id, $type = 'asign', $start = '', $end = '') {
		// Get box
		$criteria = new CDbCriteria();
		$criteria->with 		= array('member_asign');
		$criteria->condition 	= "event_fg = 1 and member_asign.member_id = :member_id";
		$params[':member_id'] 	= $member_id;

		if ($start != '') {
			$criteria->addCondition('DATE_FORMAT(t.created, "%Y%m") >= :start');
			$start	= mb_substr($start, 3, 4) . mb_substr($start, 0, 2);
			$params[':start'] 	= $start;
		}

		if ($end != '') {
			$criteria->addCondition('DATE_FORMAT(t.created, "%Y%m") <= :end');
			$end	= mb_substr($end, 3, 4) . mb_substr($end, 0, 2);
			$params[':end'] 	= $end;
		}

		if ($type == 'joined') {
			$criteria->addCondition('member_asign.event_joined = :event_joined');
			$params[':event_joined'] 	= 1;
		}
		
		$criteria->params = $params;

		return $this->findAll($criteria);

	}
	
	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('title',$this->title,true);
		$criteria->compare('content',$this->content,true);
		$criteria->compare('description',$this->description,true);
		$criteria->compare('status',$this->status);
		$criteria->compare('event_fg',$this->event_fg);
		$criteria->compare('position',$this->position);
		$criteria->compare('view_no',$this->view_no);
		$criteria->compare('join_no',$this->join_no);
		$criteria->compare('created',$this->created,true);
		$criteria->compare('updated',$this->updated,true);
		$criteria->compare('user_id',$this->user_id);
		$criteria->compare('event_end_date',$this->event_end_date,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
}